<%@ page import="edu.whut.change.auth.vo.AdminRedisVo" %>
<%@ page import="edu.whut.change.base.util.RedirectUtil" %>
<%--
  Created by IntelliJ IDEA.
  User: YTY
  Date: 2016/3/26
  Time: 18:04
  To change this template use File | Settings | File Templates.
--%>
<%
    AdminRedisVo adminInfo = (AdminRedisVo) request.getAttribute("admin");
    int userId = adminInfo.getId();
    int adminType = adminInfo.getAdminType().ordinal();
    String userName = adminInfo.getName();
    String moduleJson = adminInfo.getModuleJson();
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script src="/jsp/common/js/module/ajaxModule.js"></script>
<script src="/jsp/common/js/module/entityModule.js"></script>
<script src="/jsp/common/js/angular-md5.min.js"></script>
<script src="/jsp/common/js/angular-cookie.min.js"></script>
<script src="/jsp/common/js/util.js"></script>
<style type="text/css">
    /*
    .modal-backdrop {
        opacity: 0 !important;
        filter: alpha(opacity=0) !important;
    }
    */
</style>
<script type="text/javascript">
    var websocket = null;
    var adminType = <%=adminType%>;
    //var adminName = "<%=userName%>";
    //console.log(adminType);
    if(adminType != 1) {
        var webHost = "<%=RedirectUtil.getHost("server")%>";
        console.log(webHost);
        var serverHost = webHost.replace("https://", "");
        //console.log(webSocketIP);
        var target = 'wss://' + serverHost + '/websocket';
        //判断当前浏览器是否支持WebSocket
        if ('WebSocket' in window) {
            websocket = new WebSocket(target);
        }
        else {
            alert('Not support websocket')
        }

        //连接发生错误的回调方法
        websocket.onerror = function () {
            alert("websocket连接错误");
        };

        //连接成功建立的回调方法
        websocket.onopen = function (event) {
        }

        //接收到消息的回调方法
        websocket.onmessage = function (event) {
            if (event.data == "bksh") {
                document.getElementById('message').innerHTML = '';
                setMessageInnerHTML("有新的百科需要审核");
                $("#push").modal("show");
            } else if (event.data == "htxzzj") {
                document.getElementById('message').innerHTML = '';
                setMessageInnerHTML("有新增专家需要审核");
                $("#push").modal("show");
            } else if (event.data == "htzjxg") {
                document.getElementById('message').innerHTML = '';
                setMessageInnerHTML("有专家修改需要审核");
                $("#push").modal("show");
            } else if (event.data == "zjszxg") {
                document.getElementById('message').innerHTML = '';
                setMessageInnerHTML("有专家设置修改需要审核");
                $("#push").modal("show");
            } else if (event.data == "zjsh") {
                document.getElementById('message').innerHTML = '';
                setMessageInnerHTML("有专家需要审核");
                $("#push").modal("show");
            } else {
                alert("接收消息错误！");
            }
        }

        //连接关闭的回调方法
        websocket.onclose = function () {
        }

        //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
        window.onbeforeunload = function () {
            websocket.close();
        }

        //将消息显示在网页上
    function setMessageInnerHTML(innerHTML){
        document.getElementById('message').innerHTML += innerHTML + '<br/>';
    }
        //关闭连接
    function
    closeWebSocket(){
        websocket.close();
    }

    //发送消息
        /* function send(){
        var message = document.getElementById('text').value;
        websocket.send(message);
    }
     */
    }

</script>
<script type="text/javascript">
    function toEdit(){
        var text = document.getElementById("message").innerHTML;
        text=text.replace("<br>","");
        //alert(text);
        if(text == "有新的百科需要审核"){
            window.location.href = "/jsp/admin/agent/baike_edit.jsp" ;
        }
        else if(text == "有专家设置修改需要审核" || text == "有专家需要审核"){
            window.location.href = "/jsp/expert/expert_edit.jsp" ;
        }else{
            window.location.href = "/jsp/admin/agent/expert_edit.jsp" ;
        }
    }
</script>
<script>
    var userId = <%=userId%>;
    var adminType = <%=adminType%>;
    var fileHost = "<%=RedirectUtil.getHost("file")%>";
    var nm = angular.module("nm", ["ajaxModule", "ipCookie", "filterModule", "angular-md5", 'entityModule']);
    nm.controller("nc", function ($scope, $rootScope, ajax, ipCookie, $location, md5, entity) {
        $scope.userName = "<%=userName%>";
        $scope.navs = <%=moduleJson%>;
        $scope.collapse = $location.search().collapse && $location.search().collapse == "true";
        if ($scope.collapse) {
            $("body").addClass("nav-collapse");
        }
        $scope.collapseF = function () {
            $scope.collapse = !$scope.collapse;
            $location.search("collapse", $scope.collapse ? "true" : "false");
            if ($scope.collapse) {
                $("body").addClass("nav-collapse");
            } else {
                $("body").removeClass("nav-collapse");
            }
        };
        $scope.collapseNav = function (index) {
            var nav = $scope.navs[index];
            nav.in = !nav.in;
            $("#collapse-" + nav.name).collapse(nav.in ? "show" : "hide");
        }
        $scope.logout = function () {
            ajax.ajax("/server/login/logoutAdmin", "POST", {
                token: ipCookie("token")
            }).success(function (data) {
                ipCookie.remove("token");
                window.location.href = "/";
            })
        };
        $scope.add = function (entity, success, error) {
            ajax.ajax("/server/admin/updateAdminPwd", "POST", {
                userId: userId,
                origin: md5.createHash(entity.origin),
                password: md5.createHash(entity.password)
            }).success(function (data) {
                if (data.success) {
                    success();
                    ipCookie.remove("token");
                    window.location.href = "/";
                } else {
                    error(data.message);
                }
            })
        };
        $scope.entity = entity.getEntity({
            origin: "",
            password: "",
            password2: ""
        }, {
            origin: {
                v: function (entity) {
                    return validate.password.v(entity.origin);
                },
                t: validate.password.t
            },

            password: {
                v: function (entity) {
                    return validate.password.v(entity.password);
                },
                t: validate.password.t
            },
            password2: {
                v: function (entity) {
                    return entity.password == entity.password2;
                },
                t: "两次密码不一致"
            }
        }, function () {
        }, $scope.add, function () {
        }, function () {
        }, "pwdModel");
        var sideScroll = false;
        var preventDefault = function (e) {
            e.preventDefault();
        };
        $(".sidebar").on("mouseenter", function () {
            window.addEventListener('mousewheel', preventDefault);
        });
        $(".sidebar").on("mouseleave", function () {
            window.removeEventListener('mousewheel', preventDefault);
        });
        document.getElementById("sidebar-container").addEventListener("mousewheel", function (e) {
            if (sideScroll) {
                return;
            }
            var sidebar = $(".sidebar");
            var sidebarContainer = $("#sidebar-container");
            var floor = sidebar.height() - sidebarContainer.height();
            floor = floor > 0 ? 0 : floor;
            var topOrigin = sidebarContainer.css("margin-top");
            topOrigin = parseInt(topOrigin.substring(0, topOrigin.length - 2));
            var top = topOrigin - e.deltaY;
            top = top > 0 ? 0 : top;
            top = top < floor ? floor : top;
            if (top != topOrigin) {
                sideScroll = true;
                sidebarContainer.animate({"marginTop": top}, "fast", "swing", function () {
                    sideScroll = false;
                });
            }
        })
    })
</script>
<link type="text/css" rel="stylesheet" href="/jsp/common/css/nav.css">
<div ng-controller="nc">
    <div entity-modal="pwdModel" title="密码" entity="entity">
        <div entity-control-text="origin" type="password" title="原密码" entity="entity.entity"
             validate="entity.validate"></div>
        <div entity-control-text="password" type="password" title="密码" entity="entity.entity"
             validate="entity.validate"></div>
        <div entity-control-text="password2" type="password" title="确认密码" entity="entity.entity"
             validate="entity.validate"></div>
    </div>
</div>
<nav class="navbar navbar-fixed-top" ng-controller="nc">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand logo" href="/jsp/parlor/parlor.jsp">
                <img class="img-rounded img-responsive" alt="Brand"
                     src="/jsp/common/img/logo.png">
            </a>
            <a class="navbar-brand brand" href="/jsp/parlor/parlor.jsp">
                <img class="img-rounded img-responsive" alt="Brand"
                     src="/jsp/common/img/brand.png">
            </a>
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse"
                    aria-expanded="false">
                <span class="glyphicon glyphicon-bar"></span>
                <span class="glyphicon glyphicon-bar"></span>
                <span class="glyphicon glyphicon-bar"></span>
            </button>
        </div>
        <div class="collapse navbar-collapse" id="navbar-collapse">
            <ul class="nav navbar-right navbar-nav">
                <li>
                    <a class="dropdown-toggle" data-toggle="dropdown" role="button">
                        <span ng-bind="userName"></span> <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu">
                        <li>
                            <a href="javascript:void(0)" class="p-y" ng-click="entity.openAdd()">
                                <span class="icon-edit m-r"></span> 修改密码</a>
                        </li>
                        <li class="p-y">
                            <a href="javascript:void(0)" class="p-y" ng-click="logout()">
                                <span class="icon-signout m-r"></span> 退出</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>

</nav>
<div class="sidebar" ng-controller="nc" ng-class='{"collapse":collapse}'>
    <div id="sidebar-container">
        <ul class="nav nav-sidebar">
            <li>
                <a href="javascript:void(0)" ng-click="collapseF()" class="text-center hover">
                    <span ng-class='{"icon-align-justify":!collapse, "icon-list":collapse}'></span>
                </a>
            </li>
        </ul>
        <ul class="nav nav-sidebar" ng-repeat="nav in navs"
            ng-class='{"nav-line":nav.index==40||nav.index==60||nav.index==120}'>
            <li ng-class='{"active":index == nav.name && !nav.subs.length}'>
                <a ng-if="!nav.subs.length" ng-href="{{nav.url}}"
                   ng-mouseenter="nav.hover = true" ng-mouseleave="nav.hover = false">
                    <span class="{{nav.icon}}"></span>
                    <span class="m-l-lg" ng-bind="nav.name" ng-class='{"hide":collapse}'></span>
                </a>
                <a ng-if="nav.subs.length" ng-click='collapseNav($index)'
                   ng-init="nav.in = index == nav.name" href="javascript:void(0)"
                   ng-mouseenter="nav.hover = true" ng-mouseleave="nav.hover = false">
                    <span ng-class='{"icon-chevron-down":nav.in, "icon-chevron-right":!nav.in}'></span>
                    <span class="m-l-lg" ng-bind="nav.name" ng-class='{"hide":collapse}'></span>
                </a>

                <div ng-show="nav.hover && collapse" class="nav-tooltip ng-hide" ng-bind="nav.name"></div>
            </li>
            <ul ng-if="nav.subs.length" ng-class='{"collapse":nav.subs.length,"in":nav.in}'
                id='{{"collapse-"+nav.name}}' class="nav nav-sidebar-sub">
                <li ng-repeat="sub in nav.subs"
                    ng-class='{"active":nav.name == index && subIndex == sub.name}'>
                    <a ng-href="{{sub.url}}" ng-mouseenter="sub.hover = true" ng-mouseleave="sub.hover = false">
                        <span class="{{sub.icon}}"></span>
                        <span class="m-l-lg" ng-bind="sub.name" ng-class='{"hide":collapse}'></span>
                    </a>

                    <div ng-show="sub.hover && collapse" class="nav-tooltip ng-hide" ng-bind="sub.name"></div>
                </li>
            </ul>
        </ul>
    </div>
</div>
<div class="modal fade" id="push">
    <div class="modal-dialog" style="width:200px;position:absolute;right:0px;bottom:0px;height:100px;">
        <div class="modal-content">
            <div class="modal-header" style="height: 40px;">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title"><strong>审核提示</strong></h4>
            </div>
            <div class="modal-body" align="center" style="height: auto;" id="message">

            </div>
            <div class="modal-footer" style="height: 50px;">
                <button class="btn btn-primary btn-xs" type="submit" id="edit" onclick="toEdit()">审核</button>
                <button class="btn btn-danger btn-xs" type="button" data-dismiss="modal">忽略</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
